package controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import domain.User;

import javax.servlet.http.HttpServletRequest;

@Controller
public class PageController {
    @RequestMapping("/register")
    public String showPageByVoid() {
        System.out.println("showPageByVoid running");
        return "register";
    }

    @RequestMapping("/showPageByForward")
    public String showPageByForward() {
        System.out.println("showPageByForward running");
        return "forward:index.jsp";
    }

    @RequestMapping("/showPageByRedirect")
    public String showPageByRedirect() {
        System.out.println("showPageByRedirect running");
        return "redirect:http://www.itheima.com";
    }

//    只展示了showPageByRequest()方法

    @RequestMapping("/showPageByRequest")
    public String showPageByRequest(HttpServletRequest request) {
        System.out.println("showPageByRequest running");
        request.setAttribute("username", "request");
        return "register";
    }

//    只展示了showPageByRequest()方法

    @RequestMapping("/showPageByModel")
    public String showPageByModel(Model model) {
        System.out.println("showPageByModel running");
        model.addAttribute("username", "request");
//        model.addAttribute("password", "123456");
        return "register";
    }

    @RequestMapping("/showModelAndView")
    public ModelAndView showModelAndView() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("username","heima");
        User user = new User(); user.setPassword("password");
        modelAndView.addObject("user",user);
        modelAndView.setViewName("register");
        return modelAndView;
    }

}
